package com.baili.controller.distribution;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baili.dto.BaseTransferEntity;
import com.baili.dto.ProductOrder;
import com.baili.service.distribution.DistributionService;
import com.baili.util.MobileConfig;

/** 
 * @author iceX
 * @version 创建时间：2016年3月30日 下午2:19:29 
 * @Name: DistributionController.java  配帮人员
 */
@Controller
@RequestMapping(value = "")
public class DistributionController {
	
	private static Logger log = Logger.getLogger(DistributionController.class);
	@Autowired
	private DistributionService distributionService;
	
	/**
	 * 配帮人员
	 * @param request
	 * @param response
	 * @return
	 */ 
	@RequestMapping(value = "/service/getDistributionList")
	@ResponseBody
	public BaseTransferEntity getDistributionList(HttpServletRequest request,
			HttpServletResponse response) {
		BaseTransferEntity baseTransferEntity = new BaseTransferEntity();
		try {
			int matchingStaffType = Integer.parseInt(request.getParameter("matchingStaffType"));
			String scheduleNo = request.getParameter("scheduleNo");
			ProductOrder productOrder = new ProductOrder();
			productOrder.setMatchingStaffType(matchingStaffType);
			productOrder.setScheduleNo(scheduleNo);
			//获取列表数据
			
			List<Map<String, Object>> lst = distributionService.getDistributionListBPOI(productOrder);
			
			baseTransferEntity.setResultcode(MobileConfig.getStringCode("code.global.success"));
			baseTransferEntity.setData(lst);
			baseTransferEntity.setDesc(MobileConfig.getString("msg.global.success"));
			
		} catch (Exception e) {
			log.error("--------->" + e.getMessage(), e);
			baseTransferEntity.setResultcode(MobileConfig.getStringCode("code.global.error.exception"));
			baseTransferEntity.setDesc("系统接口异常，请联系管理员！");
			baseTransferEntity.setData("");
		}
		
		return baseTransferEntity;
	}
	
	/**
	 * 可配帮详情（通过生产通知单）
	 * @param request
	 * @param response
	 * @return
	 */ 
	@RequestMapping(value = "/service/getDistributionDetail")
	@ResponseBody
	public BaseTransferEntity getDistributionDetail(HttpServletRequest request,
			HttpServletResponse response) {
		BaseTransferEntity baseTransferEntity = new BaseTransferEntity();
		try {
			int matchingStaffType = Integer.parseInt(request.getParameter("matchingStaffType"));
			String productName = request.getParameter("productName");
			String scheduleNo = request.getParameter("scheduleNo");
			
			ProductOrder productOrder = new ProductOrder();
			productOrder.setMatchingStaffType(matchingStaffType);
			productOrder.setFinishedProductName(productName);
			productOrder.setScheduleNo(scheduleNo);
			
			//获取列表数据
			List<Map<String, Object>> lst = distributionService.getDistributionListBPOIP(productOrder);
			
			//返回列表数据
			if(lst == null || lst.size() < 1){
				baseTransferEntity.setResultcode(MobileConfig.getStringCode("code.global.error.list"));
				baseTransferEntity.setData(null);
				baseTransferEntity.setDesc(MobileConfig.getString("msg.global.error.list"));
				return baseTransferEntity;
			}
			baseTransferEntity.setResultcode(MobileConfig.getStringCode("code.global.success"));
			baseTransferEntity.setData(lst);
			baseTransferEntity.setDesc(MobileConfig.getString("msg.global.success"));
			
		} catch (Exception e) {
			log.error("--------->" + e.getMessage(), e);
			baseTransferEntity.setResultcode(MobileConfig.getStringCode("code.global.error.exception"));
			baseTransferEntity.setDesc("系统接口异常，请联系管理员！");
			baseTransferEntity.setData("");
		}
			
		
		return baseTransferEntity;
	}
	
	/**
	 * 更新可配帮（通过生产通知单）
	 * @param request
	 * @param response
	 * @return
	 */ 
	@RequestMapping(value = "/service/updateDistribution")
	@ResponseBody
	public BaseTransferEntity updateDistribution(HttpServletRequest request,
			HttpServletResponse response) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		BaseTransferEntity baseTransferEntity = new BaseTransferEntity();
		try {
			int size = Integer.parseInt(request.getParameter("size"));
			int matchingStaffType = Integer.parseInt(request.getParameter("matchingStaffType"));
			int inputCount = Integer.parseInt(request.getParameter("realCount"));
			
			String productCode = request.getParameter("productCode");
			String scheduleNo = request.getParameter("scheduleNo");
			
			ProductOrder productOrder = new ProductOrder();
			productOrder.setSize(size);
			productOrder.setMatchingStaffType(matchingStaffType);
			productOrder.setMatchingStaffCount(inputCount);
			productOrder.setProductCode(productCode);
			productOrder.setScheduleNo(scheduleNo);
			productOrder.setMatchingStaffTime(sdf.format(new Date()));
			//获取列表数据
			distributionService.updateDistribution(productOrder);
			
			baseTransferEntity.setIsSuccess(0);
			baseTransferEntity.setResultcode(MobileConfig.getStringCode("code.global.success"));
			baseTransferEntity.setData(null);
			baseTransferEntity.setDesc(MobileConfig.getString("msg.global.success"));
			
		} catch (Exception e) {
			log.error("--------->" + e.getMessage(), e);
			baseTransferEntity.setResultcode(MobileConfig.getStringCode("code.global.error.exception"));
			baseTransferEntity.setDesc("系统接口异常，请联系管理员！");
			baseTransferEntity.setData("");
		}
			
		
		return baseTransferEntity;
	}
	
	/**
	 * 可配帮详情(通过定额领料单)
	 * @param request
	 * @param response
	 * @return
	 */ 
	@RequestMapping(value = "/service/getDistributionByNormed")
	@ResponseBody
	public BaseTransferEntity getDistributionByNormed(HttpServletRequest request,
			HttpServletResponse response) {
		BaseTransferEntity baseTransferEntity = new BaseTransferEntity();
		try {
			String productCode = request.getParameter("productCode");
			String scheduleNo = request.getParameter("scheduleNo");
			
			ProductOrder productOrder = new ProductOrder();
			productOrder.setProductCode(productCode);
			productOrder.setScheduleNo(scheduleNo);
			productOrder.setMatchingStaffType(0);
			//获取列表数据
			int y_matchingStaff = 0;
			List<Map<String, Object>> lst = distributionService.getDistributionByNormed(productOrder);
//			for (Map<String, Object> map : lst) {
//				y_matchingStaff = y_matchingStaff + Integer.parseInt(String.valueOf(map.get("quotaCount")));
//			}
			//查询领料单中所有帮面数量未配帮
			int normedCountBN = 0;
			//查询领料单中所有中底数量未配帮
			int normedCountZN = 0;
			//查询领料单中所有成型底数量未配帮
			int normedCountCN = 0;
			List<Map<String, Object>> normedCountBNLst = distributionService.getNormedCountB(productOrder);
			
			List<Map<String, Object>> normedCountZNLst = distributionService.getNormedCountZ(productOrder);
			
			List<Map<String, Object>> normedCountCNLst = distributionService.getNormedCountC(productOrder);
			
			if(normedCountBNLst != null && normedCountBNLst.size() > 0){
				
				Map<String, Object> normedCountBNMap = normedCountBNLst.get(0);
				
				if(normedCountBNMap != null){
					
					normedCountBN = normedCountBN + Integer.parseInt(normedCountBNMap.get("quotaCount").toString());
				}
				
			}
			
			if(normedCountZNLst != null && normedCountZNLst.size() > 0){
				
				Map<String, Object> normedCountZNMap = normedCountZNLst.get(0);
				if(normedCountZNMap != null){
					
					normedCountZN = normedCountZN + Integer.parseInt(normedCountZNMap.get("quotaCount").toString());
				}
			}

			if(normedCountCNLst != null && normedCountCNLst.size() > 0){
				
				Map<String, Object> normedCountCNMap = normedCountCNLst.get(0);
				if(normedCountCNMap != null){
					
					normedCountCN = normedCountCN + Integer.parseInt(normedCountCNMap.get("quotaCount").toString());
				}
			}
			
			productOrder.setMatchingStaffType(1);
			
			//查询领料单中所有帮面数量已配帮
			int normedCountBY = 0;
			//查询领料单中所有中底数量已配帮
			int normedCountZY = 0;
			//查询领料单中所有成型底数量已配帮
			int normedCountCY = 0;
			
			
			List<Map<String, Object>> normedCountBYLst = distributionService.getNormedCountB(productOrder);
			List<Map<String, Object>> normedCountZYLst = distributionService.getNormedCountZ(productOrder);
			List<Map<String, Object>> normedCountCYLst = distributionService.getNormedCountC(productOrder);
			
			if(normedCountBYLst != null && normedCountBYLst.size() > 0){
				
				Map<String, Object> normedCountBYMap = normedCountBYLst.get(0);
				if(normedCountBYMap != null){
					
					normedCountBY = normedCountBY + Integer.parseInt(normedCountBYMap.get("quotaCount").toString());
				}
			}
			
			if(normedCountZYLst != null && normedCountZYLst.size() > 0){
				
				Map<String, Object> normedCountZYMap = normedCountZYLst.get(0);
				if(normedCountZYMap != null){
					
					normedCountZY = normedCountZY + Integer.parseInt(normedCountZYMap.get("quotaCount").toString());
				}
			}

			if(normedCountCYLst != null && normedCountCYLst.size() > 0){
				
				Map<String, Object> normedCountCYMap = normedCountCYLst.get(0);
				
				if(normedCountCYMap != null){
				
					normedCountCY = normedCountCY + Integer.parseInt(normedCountCYMap.get("quotaCount").toString());
				}
			}
			
			int normedCountB = normedCountBN+normedCountBY;
			int normedCountZ = normedCountZN+normedCountZY;
			int normedCountC = normedCountCN+normedCountCY;
			
			int matchingStafftotalCount = Math.max(Math.max(normedCountB,normedCountZ),normedCountC);
			
			y_matchingStaff = normedCountBY;
			
			int matchingStaffTypeCount = Math.min(Math.min(normedCountBN,normedCountZN),normedCountCN);
			
			int noMatchingStaffTypeCount = Math.max(Math.max(normedCountBN,normedCountZN),normedCountCN);
			
			//返回列表数据
			if(lst == null || lst.size() < 1){
				baseTransferEntity.setResultcode(MobileConfig.getStringCode("code.global.error.list"));
				baseTransferEntity.setData(null);
				baseTransferEntity.setDesc(MobileConfig.getString("msg.global.error.list"));
				return baseTransferEntity;
			}
			
			for(Map<String, Object> map : lst){
				
				map.put("y_matchingStaff", y_matchingStaff);
				map.put("matchingStafftotalCount", matchingStafftotalCount);
				map.put("noMatchingStaffTypeCount", noMatchingStaffTypeCount);
				map.put("matchingStaffTypeCount", matchingStaffTypeCount);
				
			}
			baseTransferEntity.setResultcode(MobileConfig.getStringCode("code.global.success"));
			baseTransferEntity.setData(lst);
			baseTransferEntity.setDesc(MobileConfig.getString("msg.global.success"));
			
		} catch (Exception e) {
			log.error("--------->" + e.getMessage(), e);
			baseTransferEntity.setResultcode(MobileConfig.getStringCode("code.global.error.exception"));
			baseTransferEntity.setDesc("系统接口异常，请联系管理员！");
			baseTransferEntity.setData("");
		}
			
		
		return baseTransferEntity;
	}
	
	/**
	 * 更新可配帮（通过定额领料单）
	 * @param request
	 * @param response
	 * @return
	 */ 
	@RequestMapping(value = "/service/updateDistributionByNormed")
	@ResponseBody
	public BaseTransferEntity updateDistributionByNormed(HttpServletRequest request,
			HttpServletResponse response) {
		BaseTransferEntity baseTransferEntity = new BaseTransferEntity();
		try {
			int size = Integer.parseInt(request.getParameter("size"));
			int realCount = Integer.parseInt(request.getParameter("realCount"));
			String productCode = request.getParameter("productCode");
			String scheduleNo = request.getParameter("scheduleNo");
			
			ProductOrder productOrder = new ProductOrder();
			productOrder.setSize(size);
			productOrder.setRealCound(realCount);
			productOrder.setProductCode(productCode);
			productOrder.setScheduleNo(scheduleNo);
			productOrder.setMatchingStaffType(1);
			
			List<Map<String,Object>> lst = distributionService.getDistributionBySize(productOrder);
			
			if(lst != null && lst.size() > 0){
				
				Map<String, Object> map = lst.get(0);
				
				realCount = Integer.parseInt(map.get("realCount").toString());
				
				productOrder.setRealCound(realCount);
			}
			
			//获取列表数据
			distributionService.updateDistributionByNormed(productOrder);
			
			//返回列表数据
			baseTransferEntity.setIsSuccess(0);
			baseTransferEntity.setResultcode(MobileConfig.getStringCode("code.global.success"));
			baseTransferEntity.setData(null);
			baseTransferEntity.setDesc(MobileConfig.getString("msg.global.success"));
			
		} catch (Exception e) {
			log.error("--------->" + e.getMessage(), e);
			baseTransferEntity.setResultcode(MobileConfig.getStringCode("code.global.error.exception"));
			baseTransferEntity.setDesc("系统接口异常，请联系管理员！");
			baseTransferEntity.setData("");
		}
			
		
		return baseTransferEntity;
	}
	
}
